# -*- coding: utf-8 -*-
# @Time : 2022/7/12 10:59
# @Author : Dftre
# @Email : 3066417822@qq.com
# @File : interpolation.py
# @Project : 代码生成器
import re

from .processor import Processor, GeneratorException


class Objects(Processor):
    def __init__(self, val_dict: dict):
        super().__init__(r'\$\{(\w+)}')
        super()._add_doc("val_dict", val_dict)
        self.val_dict = val_dict

    def sub(self, text):
        vals = re.findall(r'\$\{(\w+)}', text)
        for val in vals:
            text = re.sub(r'\$\{(\w+)}', '', text)
            val_list = re.findall(r'\$\{' + val + r'[.](\w+)}', text)
            for i in val_list:
                text = (re.sub(r'\$\{' + val + '[.]' + i + '}', str(self.val_dict[val][i]), text))
        return text
